<template>
	<view class="all">
		<view>
			<!-- 2.0.19支持autoBack，默认为false -->
			<u-navbar title="个人资料" @rightClick="rightClick" :autoBack="true" rightText='保存'>
			</u-navbar>
		</view>
		<view style="height: var(--status-bar-height);">
			<!-- app状态栏高度占位 -->
		</view>
		<view class="idmsg">
			<view class="photo">
				<view class="left">
					头像
				</view>
				<view class="right iconfont">
					<view class="pimg" @click="choosePhoto()">
						<img style="width: 50px;height: 50px;border-radius: 25px;" :src="userInfo.img" alt=""> 
					</view>
					&nbsp; &#xe60d;
				</view>
			</view>
			<view class="name">
				<view class="left">
					机构名称
				</view>
				<view class="right iconfont" @click="toEditName(userInfo.companyName)">
					{{userInfo.companyName}} &nbsp;&#xe60d;
				</view>
			</view>
			
		</view>
	</view>
</template>

<script>
	import {getAdminInfo,adminDataEdition} from '@/utils/api.js'
	export default {
		data() {
			return {
				userInfo:{},
				changeName:''
			};
		},
		onLoad() {
		
		},
		onShow() {
			
			getAdminInfo().then(res=>{
				if(res.data.meta.status==200){
					this.userInfo=res.data.data
					if(this.changeName!=''){
						this.userInfo.companyName=this.changeName
					}
					try{
						let img = uni.getStorageSync('img');
						console.log(img,'img');
						if(img){
							this.userInfo.img=img
						}
					}catch(e){
						//TODO handle the exception
					}
				}
			})
			
		},
		methods:{
			otherFun(object){
			    if(!!object){
			        console.log('object=========',object)
					this.changeName=object	
			    }
			},
			toEditName(a){
				console.log(a,'aaa');
				uni.navigateTo({
					url:'/pages/my/setting/editName?'+a
				})
			},
			choosePhoto(){
				var that =this
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: function (res) {
						let urls=res.tempFilePaths[0]
						uni.uploadFile({
							url:'https://www.ghhlkj.com/api/saveFile',
							filePath: urls,
							name: 'file',
							formData: {
								user: 'test'
							},
							success: (res) => {
								console.log(JSON.parse(res.data).url,'sssssssssss',that.userInfo);
								that.userInfo.img=JSON.parse(res.data).url;
								uni.setStorageSync('img',JSON.parse(res.data).url)
								console.log(that.userInfo.img,'llllll');
							}
						});
					}
				});
			},
			rightClick(){
				let data={
					img:this.userInfo.img,
					institution:this.userInfo.companyName
				}
				console.log(data,'data');
				adminDataEdition(data).then(res=>{
					console.log(res,'?????????????');
					if (res.data.meta.status == 200) {
						uni.showToast({
							title: '修改信息成功！',
							icon: 'none'
						});
						setTimeout(()=>{
							uni.navigateBack()
						},1000)
					}			
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
.all{
		
		background-color: #f6f6f6;
		padding-top:10px ;
		.idmsg{
			margin-top: 144px;
			background-color: #ffffff;
			.photo{
				
				.right{
					display: flex;
					.pimg{
						width: 50px;
						height: 50px;
						border-radius: 25px;	
						margin-right: 8px;
						margin-top: 2.5px;
					}
				}
			}
			
			.photo,.name{
				padding: 0 10px;
				font-size: 16px;
				height: 55px;
				line-height: 55px;
				font-weight: Medium;
				display: flex;
				justify-content: space-between;
				align-items: center;
				border-bottom: 0.5px  #f6f6f6 solid;
			}
		}
	}

</style>
